﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Threading;
using System.IO;

namespace BoardAppUtil.Util
{
    /// <summary>
    ///  <title></title>
    ///  <description></description>
    ///  <copyRight>Copyright (c) 2012</copyRight>
    ///  <company>IronOne Technologies (Pvt)Ltd</company>
    ///  <createdOn>2012-</createdOn>
    ///  <author></author>
    ///  <modification>
    ///     <modifiedBy></modifiedBy>
    ///      <modifiedDate></modifiedDate>
    ///     <description></description>
    ///  </modification>
    /// </summary>
    public class FileUtility
    {

        public static void DeletePdfs(IEnumerable<string> filePathAndNames)
        {
            if (filePathAndNames.Count() > 0)
            {
                foreach (string filePath in filePathAndNames)
                    try
                    { System.IO.File.Delete(filePath); }
                    catch (Exception)
                    { }
            }
        }

        public static void DeleteZip(string zipFile)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(zipFile))
                {
                    new Thread(() =>
                          {
                              int timeWait = int.Parse(ConfigurationManager.AppSettings["ZipFileAvailability"]) * 60 * 1000;
                              Thread.Sleep(timeWait);
                              if (System.IO.File.Exists(zipFile))
                              {
                                  System.IO.File.Delete(zipFile);
                              }
                          }).Start();
                }
            }
            catch (Exception)
            {
            }
        }

        public static void VerifyDirectoryPath(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        public static byte[] ReadFromFile(string path)
        {            
            FileStream fs = null;
            try
            {
                if (!File.Exists(path))
                {
                    return null;
                }

                fs = File.OpenRead(path);
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                return bytes;
            }
            finally
            {
                fs.Close();
            }
        }
    }
}
